{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31495979-f819-4b90-a6e9-bac41809f214",
   "metadata": {},
   "source": [
    "## 第十一节、线性代数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5012c07-8101-4c5d-9f53-d457dc42494a",
   "metadata": {},
   "source": [
    "## 矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "28115680-0455-4f8b-906f-c6943419b94c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4f7705f4-e6a2-4df6-84b6-570cecbd502e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[24, 13, 15],\n",
       "       [21,  8, 14]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵的乘法\n",
    "arr1 = np.random.randint(0, 30,size=(2, 3))\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a663198d-30eb-43b0-b5d9-0b723c773993",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1, -1],\n",
       "       [-6, -3],\n",
       "       [14, -3]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.random.randint(-10, 20, size=(3, 2))\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0f9e9e17-1d1e-493e-8313-29e674bee088",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 108, -108],\n",
       "       [ 127,  -87]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dot矩阵乘法\n",
    "np.dot(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7d7cf8ec-6555-42a8-8e35-f2b5d84d5b72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 108, -108],\n",
       "       [ 127,  -87]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# @运算符也是矩阵乘法\n",
    "arr1 @ arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76e6a687-da31-495d-8173-e9b9ed642cae",
   "metadata": {},
   "source": [
    "## 矩阵其他计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1eb8c9ec-e0a2-44e8-b8ca-d3fc2ec1b892",
   "metadata": {},
   "source": [
    "下面可以计算矩阵的逆、行列式、特征值和特征向量、qr分解值、svd分解值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f4bf08d4-7868-4935-ab63-155b37c35eaa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 3, 4],\n",
       "       [4, 5, 8]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算矩阵的逆\n",
    "from numpy.linalg import inv, det, eig, qr, svd\n",
    "\n",
    "arr3 = np.array(\n",
    "    [[1, 2, 3],\n",
    "     [2, 3, 4],\n",
    "     [4, 5, 8]]\n",
    ")\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b98991e5-4cc6-4303-a843-c405f09b9b5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2. ,  0.5,  0.5],\n",
       "       [ 0. ,  2. , -1. ],\n",
       "       [ 1. , -1.5,  0.5]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(suppress=True)\n",
    "inv(arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "183ecdcb-77ee-4934-a091-d578ba462c14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2.0000000000000004"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "det(arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ba5d3ae-0781-4690-a5d5-c06e4358035c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
